<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: wheat;
            border-radius: 12px;
            position: absolute;
            left: 10px;
            top: 10px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        var box = document.querySelector('.box')
        // let boxX = getComputedStyle(操作对象,伪类)[元素]

        //可以设置transition
        function move(element, dir, end, callback) {
            var timer = setInterval(() => {
                let num = getComputedStyle(box, null)[dir]
                num = parseInt(num)
                // IE用currentStyle

                let flag = (end > num)

                let step = flag ? 1 : -1; // 设置步长
                box.style[dir] = num + step + 'px'
                if (flag) {
                    if (num >= end) {
                        clearInterval(timer)
                        callback()
                    }
                } else {
                    if (num <= end) {
                        clearInterval(timer)
                        callback()
                    }
                }
            }, 1)
        }

        function circle(callback) {
            move(box, 'left', 300, () => move(box, 'top', 300, () => move(box, 'left', 10, () => move(box, 'top', 10, () => {}))))
            if(callback){
                callback();
            }
        }

        circle(()=>console.log('注意,动画和定时器时间不一样'));
    </script>
</body>

</html>